package org.dinner.order.service.impl;

import java.util.List;

import org.dinner.order.dao.RoleDAO;
import org.dinner.order.dao.UserDAO;
import org.dinner.order.model.User;
import org.dinner.order.service.Roles;
import org.dinner.order.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private RoleDAO roleDAO;
	
	@Transactional
	public User add(User item) {
		if (item.getRole() == null) {
			item.setRole(roleDAO.getByName(Roles.USER));
		}
		return userDAO.add(item);
	}

	@Transactional
	public List<User> list() {
		return userDAO.list();
	}

	@Transactional
	public User getById(int id) {
		return userDAO.getById(id);
	}

	@Transactional
	public void remove(int id) {
		userDAO.remove(id);
	}
	
	@Transactional
	public User getByLogin(String login) {
		return userDAO.getByLogin(login);
	}

}
